<?php
/**
 * Created by PhpStorm.
 * 用户行为操作
 * User: caicm
 * Date: 2017/3/9
 * Time: 11:16
 */

namespace SDXApi\Service;


class UserBehavior {

    /*
     * 添加地址到操作记录表
     * Created By caicm
     * @param int $address_id 地址id
     * @Param int $page_id 1.编辑地址2.添加地址3.购物车点选4.订单页面点选
     * @param int $old_id 旧地址id
     * return int
     */
    public function add_address($address_id, $page_id, $old_id=0){

        $addres_info = M('user_address')->find($address_id);

        $addres_info['old_id'] = $old_id;
        $addres_info['page_id'] = $page_id;
        $addres_info['source'] = get_source();
        $addres_info['add_time'] = gmtime();

        unset($addres_info['address_id']);
        unset($addres_info['default']);

        return M('user_behavior_address_operation')->add($addres_info);
    }

    /*
     * 添加地址到历史表
     * Created By caicm
     * @param int $address_id 地址id
     * return int
     */
    public function add_address_old($address_id){

        $addres_info = M('user_address')->find($address_id);
        unset($addres_info['address_id']);

        return M('user_behavior_address_old')->add($addres_info);

    }

    /*
     * 用户订单操作行为
     * Created By caicm
     * @param int $uid 用户id
     * @param int $order_id 订单id
     * @param int $type 类型：1、取消订单2、查看评价（商品详情）3、查看评价（订单详情）4、浏览订单详情页
     * @param string $content 内容
     * @param int $goods_id 商品id
     * return int
     */
    public function add_user_behavior_order($uid=0, $order_id=0, $type,  $content='', $goods_id=0){
        if(!$uid){
            return false;
        }
        $data['source'] = get_source();
        $data['version'] = I('version','');
        $data['order_id'] = $order_id;
        $data['goods_id'] = $goods_id;
        $data['type'] = $type;
        $data['uid'] = $uid;
        $data['add_time'] = gmtime();
        $data['content'] = $content;
        return M('user_behavior_order')->add($data);

    }

    /*
     * 用户购物车操作行为
     * Created By caicm
     * @param int $uid 用户id
     * @param int $goods_id 商品id
     * @param int $hehavior_type 行为类型：1、普通操作，2、再来一单
     * @param string $operate 操作类型：默认为增加add；减少dec；删除del;
     * @param number $num 操作数量
     * return int
     */
    public function add_user_behavior_cart($uid=0, $goods_id=0, $hehavior_type, $operate='add',  $num){

        //获取剩余数量
        $surplus_num = M('cart')->where(array('user_id'=>$uid,'goods_id'=>$goods_id))->getField('goods_number');
        //商品名称
        $goods_name = M('goods')->where(array('goods_id'=>$goods_id))->getField('goods_name');

        if($operate == 'add'){
            $type = 1;
        }
        if($operate == 'dec'){
            $type = 2;
        }
        if($operate == "del"){
            $type = 3;
        }

        $data['source'] = get_source();
        $data['version'] = I('version','');
        $data['goods_id'] = $goods_id;
        $data['uid'] = $uid;
        $data['hehavior_type'] = $hehavior_type;
        $data['type'] = $type;
        $data['num'] = $num;
        $data['surplus_num'] = $surplus_num ? $surplus_num : 0;
        $data['goods_name'] = $goods_name;
        $data['add_time'] = gmtime();
        return M('user_behavior_cart')->add($data);

    }

    /*
     * 用户页面浏览行为
     * Created By caicm
     * @param int $uid              用户id
     * @param int $page_id 	        1.进入专题页2.分类页3.我要充值4.金额变动记录6.购物车页7.设置页8.客服页9.消息页面10.首页
     * @param int $title_id         访问名称id
     * @param string $title 	    访问名称
     * @param string $utm_source    广告来源
     * return int
     */
    public function add_browse($uid, $page_id, $title_id=0, $title='', $utm_source=''){
        $data['page_id'] = $page_id;
        $data['source'] = get_source();
        $data['version'] = I('version','');
        $data['uid'] = $uid;
        $data['add_time'] = time();
        $data['utm_source'] = $utm_source;
        $data['title_id'] = $title_id;
        $data['title'] = $title;
        return  M('userBehaviorBrowse')->add($data);
    }


    /*
     * 发现模块浏览行为
     * Created By caicm
     * @param int $uid          用户id
     * @param int $type 		操作类型:1、列表（每个页面）2、详情（阅读）3、点赞4、取消点赞5、作者文章列表
     * @param int $title_id     访问名称id
     * @param string $title 	访问名称
     * return int
     */
    public function add_community_browse($uid, $type, $title_id=0, $title=''){
        $data['type'] = $type;
        $data['source'] = get_source();
        $data['version'] = I('version','');
        $data['uid'] = $uid;
        $data['add_time'] = gmtime();
        $data['title_id'] = $title_id;
        $data['title'] = $title;
        return  M('user_behavior_community_browse')->add($data);
    }

    /*
     * 发现模块曝光行为
     * Created By caicm
     * @param int $uid          用户id
     * @param int $type 		操作类型:1、模块文章列表2、作者文章列表
     * @param array $list       文章列表
     * return int
     */
    public function add_community_exposure($uid, $type, $list){
        $source = get_source();
        $data = array();
        foreach($list as $key=>$value){
            $data[$key]['type'] = $type;
            $data[$key]['source'] = $source;
            $data[$key]['version'] = I('version','');
            $data[$key]['uid'] = $uid;
            $data[$key]['add_time'] = gmtime();
            $data[$key]['title_id'] = $value['community_id'];
            $data[$key]['title'] = $value['title'];
        }
        return  M('user_behavior_community_exposure')->addAll($data);
    }

}